<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%@ include file="/common/operatehistory.jsp"%>
<%
	Where where = new Where();
	Integer id = input.getInt("id", -1);
	Integer uid = token.getUid();
	List<Map> deptList = QBuilder.db("basedb").select("new map(parent as parent, hospital as hospital_id, name as dept_name, descr as hospital_name, jsonString as json)").from("Hospital").where(new Where("status=", STATUS_ENABLE).and("id=", id)).query().getResultObj();
	if (isEmpty(deptList)) {
		Tools.encryptPrint(out, new Message(30041, "没有该科室信息"));
		return;
	}
	Map deptMap = deptList.get(0);
	Map jsonM = JSONUtil.fromJson(deptMap.get("json") + "", new TypeReference<Map>(){});
	deptMap.put("address", Tools.getDesc(jsonM.get("address"), ""));
	Integer deptId = Tools.getInt(jsonM.get("dept_id") + "");
	Integer hospitalId = (Integer)deptMap.get("hospital_id");
	Map map = new HashMap();
	List<Map> depts = QBuilder.db("basedb").select("new map(deptName as title, branch as branch, (case when summary is NULL then '' else summary end) as summary, (case when img is NULL then '' else img end) as img, (case when content is NULL then '' else content end) as content, (case when imgs is NULL then '' else imgs end) as imgs, id as register_dept_id)").from("Dept").where(where.and("status = ", STATUS_ENABLE).and("id=", deptId)).query().getResultObj();
	String _branch = "-1";
	if (depts.size() > 0) {
		Map _map = depts.get(0);
		_branch = Tools.getDesc(_map.get("branch"), "-1");
		String content = Tools.getDesc(_map.get("content"));
		org.jsoup.nodes.Document d= Jsoup.parse(content);
		String summary = Tools.getDesc(_map.get("summary"), "");
		if ("".equals(summary)) {
			summary = d.body().text();
			if ("".equals(summary))
				summary = "暂无科室介绍";
			_map.put("summary", summary);
		}
	}
	Map deptShow = depts.size() == 0 ? new HashMap() : depts.get(0);
	deptShow.put("hospital_name", deptMap.get("hospital_name"));
	deptShow.put("hospital_address", deptMap.get("address"));
	deptShow.put("register_hospital_id", deptMap.get("parent"));
	deptShow.put("phone", Tools.getDesc(jsonM.get("phone"),  ""));
	deptShow.put("id", id);
	OperateHistory ohDept = new OperateHistory();
	ohDept.setUid(uid+"");
	ohDept.setHospital(hospital);
	ohDept.setOid(id + "");
	ohDept.setCategory(DEPT_CATEGORY);
	query(deptShow, ohDept);
	map.put("dept", deptShow);   // 查询的科室信息
	List<Map> list = QBuilder.db("basedb").select("new map(id as id, name as name, avatar  as avatar, case when title is null then '' else title end as title, jsonString as json, gender as gender, type as type)").from("User").where(new Where("hospital=", hospital).and("deptId=", hospitalId).and("status=", STATUS_ENABLE).and("type=", USER_EMPLOYEE).and("jsonString not like '%\"role\":\"2\"%'")).query().getResultObj();
	for (Map expert:list) {
		Map jsonMap = JSONUtil.fromJson(expert.remove("json") + "", new TypeReference<Map>(){});
		User user = User.fromMap(expert, User.class);
		expert.put("avatar", getDefaultAvatar(user));
		expert.put("dept_name", deptMap.get("dept_name"));
		expert.put("hospital_name", deptMap.get("hospital_name"));
		expert.put("specialty", Tools.getDesc(jsonMap.get("specialty"), "暂无擅长信息"));
		expert.put("introduce", Tools.getDesc(jsonMap.get("introduce"), "暂无介绍信息"));
	}
	map.put("experts", list);
	// 查询科室的新闻信息
	String branch = "ks" + hospitalId; // 由ks + 医院编号组成
	String selectContent = "new map(n.id as id , n.newsId as news_id, n.url as url, n.title as title, n.summary as summary, n.time as time, n.img as img, n.jsonString as json, n.tag as tag, n.category as category)";
	List<Map> labels = QBuilder.db("basedb").select("new map(id as label_id, name as name, content as content, queue as queue, icon as icon, parent as parent, jsonString as json, branch as branch)").from("Label").where(new Where("hospital =" , hospital).and("status = ", STATUS_ENABLE).and("category=", 3)).query().getResultObj();
	for (int i = 0; i < labels.size(); i++) {
		Map _map = labels.get(i);
		_map.put("branch", branch);
		Integer labelId = Tools.getInt(_map.get("label_id") + "", -1111);
		List<Map> newsList = QBuilder.db("basedb").select(selectContent).from("News n").where(new Where("n.hospital=", hospital).and("n.status = ", STATUS_ENABLE).and("n.branch=", branch).and("n.category=", labelId)).order("n.newsId", "desc").page(5, 1).query().getResultObj();
		OperateHistory oh = new OperateHistory();
		oh.setHospital(hospital);
		oh.setCategory(NEWS_CATEGORY);
		oh.setOperate(LOVE);
		_map.put("news", love(query(newsList, oh), -1, oh));
	}
	map.put("news", labels);
	msg.put("data", map);
	Tools.encryptPrint(out, msg);
%>
